(function($){
    var client = new com.im.common.ajax();
    var loading = false;
    var orgType = "";
    var orgList;

    var methods = {
        init: function(options) {
            var o = $.extend({
                elmIdTypeSel : "unitTypeSel",//类型
                elmIdSel : "unitSel",//组织
                callbackFunc: function(paramValValue,paramValName) {
                }
            }, options || {});

            var self = this;

            onInit(o);

            return this;
        },
        treeOnClickInner : function(self,opt){
            //加载数据
            return o.treeOnClick('1','test');
        }

    };

    function onInit(opt) {
        _initOrgType(opt);
        _initOrgSel(opt);
    };
    function _initOrgType(opt){
        var result = client.callAsJson("userService/queryOrgarizationsByCond", {"hasOrgType": "Y"});
        if(result.success){
            $("#"+opt.elmIdTypeSel).empty();
            $("#"+opt.elmIdTypeSel).append('<option value="">全部</option>');
            if(result.result != undefined){
                $.each(result.result, function (i, item) {
                    $("#"+opt.elmIdTypeSel).append('<option value="'+item.id+'">'+item.text+'</option>');
                });
            }
            $('.select2').select2();
        }else{
            toastr.error('服务器忙，查询失败！'+result.resultMsg);
        }

        //初始化事件
        $("#"+opt.elmIdTypeSel).change(function () {
            orgType = $(this).val();
            _filterOrg(opt);
        });
    }
    function _filterOrg(opt){

        if(orgList != undefined){

            if(orgType == undefined || orgType == ""){
                $("#"+opt.elmIdSel).empty();

                $("#"+opt.elmIdSel).append('<option value="">全部</option>');
                $.each(orgList, function (i, item) {
                    $("#"+opt.elmIdSel).append('<option value="'+item.orgId+'">'+item.orgName+'</option>');
                });
            }else{
                $("#"+opt.elmIdSel).empty();

                $("#"+opt.elmIdSel).append('<option value="">全部</option>');
                $.each(orgList, function (i, item) {
                    if(item.orgType == orgType){
                        $("#"+opt.elmIdSel).append('<option value="'+item.orgId+'">'+item.orgName+'</option>');
                    }
                });
            }

            //初始化事件
            $("#"+opt.elmIdSel).change(function () {
                _returnFunc(opt);
            });
            $("#"+opt.elmIdSel).trigger("change");
        }
    }
    function _initOrgSel(opt){
        //加载数据
        var result = client.callAsJson("userService/queryOrgarizationsByCond", {"forOrgList": ""});
        if (result.success) {
            $("#"+opt.elmIdSel).empty();
            //规划类型列表添加元素
            $("#"+opt.elmIdSel).append('<option value="">全部</option>');
            if(result.result != undefined){
                orgList = result.result;
                $.each(orgList, function (i, item) {
                    $("#"+opt.elmIdSel).append('<option value="'+item.orgId+'">'+item.orgName+'</option>');
                });
            }
            $('.select2').select2();
        }else{
            toastr.error('服务器忙，查询失败！'+result.resultMsg);
        }

        //初始化事件
        $("#"+opt.elmIdSel).change(function () {
            _returnFunc(opt);
        });
    }

    function _returnFunc(opt){
        var objvalue = $("#"+opt.elmIdSel).val();
        var objname = $("#"+opt.elmIdSel).find("option:selected").text();
        return opt.callbackFunc(objvalue,objname);
    };
    
    $.fn.unittypeToSelCommpent = function(method) {

        if (methods[method] && method.charAt(0) != '_') {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.pagination');
        }
    };

})(jQuery);